# -*- coding: utf-8 -*-
# @Author : zhangxuchuo
# @Date   : 2021/5/12
# @File   : R67_EG_CASE_EntMajorShareholdersEnforcementRecordCheck.py
# @E-mail : zhangxuchuo@bbdservice.com

'''eof
name:申请企业主要股东强制执行记录校验
code:R67_EG_CASE_EntMajorShareholdersEnforcementRecordCheck
tableName:
columnName:
groups:个人及企业司法校验
dependencies:EG_CASE_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import pandas as pd
import json
import datetime


def FormatData(tableName):
    try:
        eg = EG_CASE_DSJ.get("data").get(tableName)
        return eg if isinstance(eg, list) else [{}]
    except:
        return [{}]


# 统一转成datetime对象，只能转年月日的字符串
def date_format(time):
    try:
        import re
        import datetime
        r1 = re.compile(u'年|月|日')
        time = r1.sub('.', time)
        try:
            date = datetime.datetime.strptime(time, '%Y.%m.%d').strftime('%Y%m%d')
        except:
            try:
                date = datetime.datetime.strptime(time, '%Y-%m-%d').strftime('%Y%m%d')
            except:
                try:
                    date = datetime.datetime.strptime(time, '%Y/%m/%d').strftime('%Y%m%d')
                except:
                    try:
                        r = re.compile(r'\D+')
                        tmp = r.sub('', time)
                        if len(tmp) > 8:
                            tmp = tmp[:8]
                        date = datetime.datetime.strptime(tmp, '%Y%m%d').strftime('%Y%m%d')
                    except:
                        date = None
        return date
    except:
        return None


def getOrdTime(year):
    return (datetime.datetime.now() - pd.tseries.offsets.DateOffset(years=year)).strftime("%Y%m%d")


def float_num(str):
    try:
        return float(str)
    except:
        return None


null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']


def R67_EG_CASE_EntMajorShareholdersEnforcementRecordCheck():
    zhixingList = FormatData("SR228")
    if zhixingList == [{}]:
        return u'缺失值'
    else:
        if len(zhixingList) >= 1:
            duplicate_list = []
            resultList = []
            for i in zhixingList:
                EXECMONEY = float_num(i.get("EXECMONEY"))
                CASECODE = i.get("CASECODE")
                CASECREATETIME = i.get("CASECREATETIME")
                if date_format(CASECREATETIME) not in null_type_list and float(date_format(CASECREATETIME)) < float(getOrdTime(5)):
                    resultList.append(1)
                    continue
                if EXECMONEY not in null_type_list and CASECODE not in null_type_list and \
                        CASECREATETIME not in null_type_list and \
                        (CASECREATETIME + CASECODE) not in duplicate_list and EXECMONEY > 0:
                    duplicate_list.append(CASECREATETIME + CASECODE)
                    if date_format(CASECREATETIME) in null_type_list:
                        resultList.append(2)
                    elif float(date_format(CASECREATETIME)) > float(getOrdTime(0)):
                        resultList.append(2)
                    elif float(getOrdTime(2)) <= float(date_format(CASECREATETIME)) < float(getOrdTime(0)):
                        if EXECMONEY < 6000:
                            resultList.append(1)
                        else:
                            resultList.append(0)
                    elif float(getOrdTime(3)) <= float(date_format(CASECREATETIME)) < float(getOrdTime(2)):
                        if EXECMONEY <= 15000:
                            resultList.append(1)
                        else:
                            resultList.append(0)
                    elif float(getOrdTime(5)) <= float(date_format(CASECREATETIME)) < float(getOrdTime(3)):
                        if EXECMONEY <= 25000:
                            resultList.append(1)
                        else:
                            resultList.append(0)
                elif EXECMONEY in null_type_list or EXECMONEY <= 0 \
                        or CASECODE in null_type_list or CASECREATETIME in null_type_list:
                    resultList.append(2)
            if len(resultList) > 0:
                if min(resultList) == 0:
                    return 0
                elif max(resultList) == 2:
                    return 2
                else:
                    return 1
            else:
                return 1
        else:
            return 1


# if __name__ == "__main__":
#     import json
#     with open('C:/Users/zdr/Desktop/samples/EG_CASE_DSJ.json') as ff:
#         EG_CASE_DSJ = json.loads(ff.read())['EG_CASE_DSJ']
#     res = R67_EG_CASE_EntMajorShareholdersEnforcementRecordCheck()
#     print(res)
result = R67_EG_CASE_EntMajorShareholdersEnforcementRecordCheck()
